package com.charon.common.db;

/**
 * @Description:   redis测试类 
 * @author charon [248135724@qq.com]
 * @date:   2018年9月1日 下午7:35:49   
 */

import java.util.Set;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;

public class RedisService
{

	private static Jedis jedis;
	private JedisConnectionFactory jedisConnectionFactory;

	private Jedis getJedis()
	{
		if (jedis == null)
			return jedisConnectionFactory.getShardInfo().createResource();
		else
			return jedis;
	}

	public void del(byte key[])
	{
		getJedis().del(new byte[][] {
			key
		});
	}

	public void del(String key)
	{
		getJedis().del(new String[] {
			key
		});
	}

	public void set(byte key[], byte value[], int liveTime)
	{
		set(key, value);
		getJedis().expire(key, liveTime);
	}

	public void set(String key, String value, int liveTime)
	{
		set(key, value);
		getJedis().expire(key, liveTime);
	}

	public void set(String key, String value)
	{
		getJedis().set(key, value);
	}

	public void set(byte key[], byte value[])
	{
		getJedis().set(key, value);
	}

	public String get(String key)
	{
		String value = getJedis().get(key);
		return value;
	}

	public byte[] get(byte key[])
	{
		return getJedis().get(key);
	}

	public Set keys(String pattern)
	{
		return getJedis().keys(pattern);
	}

	public boolean exists(String key)
	{
		return getJedis().exists(key).booleanValue();
	}

	public String flushDB()
	{
		return getJedis().flushDB();
	}

	public long dbSize()
	{
		return getJedis().dbSize().longValue();
	}

	public String ping()
	{
		return getJedis().ping();
	}

	private RedisService()
	{
	}
}
